package com.example.api.service.impl;

import com.example.api.dao.DistributionDao;
import com.example.api.dao.DriverDao;
import com.example.api.dao.TollDao;
import com.example.api.dao.TollRuleDao;
import com.example.api.dao.VehicleDao;
import com.example.api.model.constants.LogisticsConstants;
import com.example.api.model.entity.Distribution;
import com.example.api.model.entity.Driver;
import com.example.api.model.entity.Toll;
import com.example.api.model.entity.TollRule;
import com.example.api.model.entity.Vehicle;
import com.example.api.model.support.RespBean;
import com.example.api.service.DistributionService;
import com.example.api.utils.BigDecimalUtil;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DistributionServiceImpl implements DistributionService {

    @Resource
    private DistributionDao distributionDao;

    @Resource
    private DriverDao driverDao;

    @Resource
    private VehicleDao vehicleDao;

    @Resource
    private TollDao tollDao;

    @Resource
    private TollRuleDao tollRuleDao;

    @Override
    public RespBean save(Distribution distribution) throws Exception {
        Distribution result = distributionDao.selectByPrimaryKey(distribution.getId());
        if (Objects.nonNull(result)) {
            return RespBean.error("配送已存在");
        }
        Driver driver = driverDao.selectByPrimaryKey(distribution.getDid());
        Vehicle vehicle = vehicleDao.selectByPrimaryKey(distribution.getVid());
        if (Objects.isNull(driver)) {
            return RespBean.error("驾驶员不存在");
        }
        if (Objects.isNull(vehicle)) {
            return RespBean.error("车辆不存在");
        }
        if (!LogisticsConstants.DRIVER_DRIVING_1.equals(driver.getDriving())) {
            return RespBean.error("驾驶员状态不符合");
        }
        if (!LogisticsConstants.VEHICLE_DRIVING_1.equals(vehicle.getDriving())) {
            return RespBean.error("车辆状态不符合");
        }
        Driver newDriver = new Driver();
        newDriver.setId(distribution.getDid());
        newDriver.setDriving(LogisticsConstants.DRIVER_DRIVING_0);
        driverDao.updateByPrimaryKeySelective(newDriver);
        Vehicle newVehicle = new Vehicle();
        newVehicle.setId(distribution.getVid());
        newVehicle.setDriving(LogisticsConstants.VEHICLE_DRIVING_0);
        vehicleDao.updateByPrimaryKeySelective(newVehicle);
        if (distributionDao.insertSelective(distribution) > 0) {
            Toll toll = new Toll();
            TollRule tollRule = tollRuleDao.selectByPrimaryKey(distribution.getSize());
            BigDecimal total = tollRule.getPrice();
            if (LogisticsConstants.DISTRIBUTION_URGENT_0.equals(distribution.getUrgent())) {
                TollRule rule = tollRuleDao.selectByPrimaryKey(4);
                total = BigDecimalUtil.add(total, rule.getPrice());
            }
            toll.setPrice(total);
            toll.setPay(LogisticsConstants.TOLL_PAY_0);
            toll.setDid(distribution.getId());
            toll.setCreateTime(new Date());
            tollDao.insertSelective(toll);
            return RespBean.success("添加配送成功");
        }
        return RespBean.error("添加配送失败");
    }

    @Override
    public List<Distribution> findAll() {
        return distributionDao.findAll();
    }

    @Override
    public Map<String, Object> can() {
        Map<String, Object> maps = Maps.newHashMap();
        maps.put("drivers", driverDao.findAll(""));
        maps.put("vehicles", vehicleDao.findAll());
        return maps;
    }

    @Override
    public RespBean update(Distribution distribution) {
        if (distributionDao.updateByPrimaryKeySelective(distribution) > 0) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @Override
    public RespBean updateStatus(Distribution distribution) {
        if (LogisticsConstants.DISTRIBUTION_STATUS_MINUS_1.equals(distribution.getStatus())) {
            Toll toll = tollDao.selectByDid(distribution.getId());
            if (LogisticsConstants.TOLL_PAY_1.equals(toll.getPay())) {
                return RespBean.error("该订单已支付，无法不通过");
            }
        }
        if (LogisticsConstants.DISTRIBUTION_STATUS_1.equals(distribution.getStatus())) {
            Toll result = tollDao.selectByDid(distribution.getId());
            if (LogisticsConstants.TOLL_PAY_0.equals(result.getPay())) {
                return RespBean.error("该订单未支付，请支付后再通过");
            }
        }
        if (LogisticsConstants.DISTRIBUTION_STATUS_2.equals(distribution.getStatus())
                || LogisticsConstants.DISTRIBUTION_STATUS_MINUS_1.equals(distribution.getStatus())) {
            Driver driver = new Driver();
            driver.setId(distribution.getDid());
            driver.setDriving(LogisticsConstants.DRIVER_DRIVING_1);
            driverDao.updateByPrimaryKeySelective(driver);
            Vehicle vehicle = new Vehicle();
            vehicle.setId(distribution.getVid());
            vehicle.setDriving(LogisticsConstants.VEHICLE_DRIVING_1);
            vehicleDao.updateByPrimaryKeySelective(vehicle);
        }
        if (distributionDao.updateByPrimaryKeySelective(distribution) > 0) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

}